package com.pcbly.web.action.dict;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.pcbly.web.action.BaseAction;
import com.pcbly.web.bean.dict.DictCategoryValueBean;
import com.pcbly.web.business.dict.DictCategoryValueService;
import com.pcbly.web.components.utils.random.GUID;

public class DictionaryValueAction extends BaseAction<DictCategoryValueBean> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7835820720609226362L;
	
	private DictCategoryValueService dictCategoryValueService;
	
	private List<DictCategoryValueBean> dictCategoryValueBeanList;
	
	
	
	public List<DictCategoryValueBean> getDictCategoryValueBeanList() {
		return dictCategoryValueBeanList;
	}
	public void setDictCategoryValueBeanList(
			List<DictCategoryValueBean> dictCategoryValueBeanList) {
		this.dictCategoryValueBeanList = dictCategoryValueBeanList;
	}
	public String dwrDelete(String valueId) throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("login deleteValue..........");
			log.debug(valueId);
		}
		dictCategoryValueService.removeDictCategoryValueById(valueId);
		return super.dwrBaseDelete(valueId);
	}
	/**
	 * 显示子菜单列表
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public List<DictCategoryValueBean> treeChild(String id) throws Exception {
		// dictCategoryValueBeanList=dictCategoryValueService.queryListById(id);
		if (log.isDebugEnabled()) {
			log.debug("login treeChild..........");
			log.debug(id);
		}
		return dictCategoryValueService.queryListById(id);
	}
	
	/**
	 * 显示子菜单详细信息
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public DictCategoryValueBean treeValueDetail(String id) throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("login treeValueDetail..........");
			log.debug(id);
		}
		// dictCategoryBean=dictCategoryService.findById(id);
		return dictCategoryValueService.findById(id);
	}
	/**
	 * 保存子菜单
	 * 
	 * @param bean
	 * @return
	 * @throws Exception
	 */
	public String dwrSave(DictCategoryValueBean bean)
			throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("login save..........");
			log.debug(bean.getId());
		}
		if (bean.getId() == null || "".equals(bean.getId())) {// new
			GUID guid = new GUID();
			bean.setId(guid.toString());
			dictCategoryValueService.createDictCategoryValue(bean);
		} else {// update
			dictCategoryValueService.updateDictCategoryValue(bean);
		}
		return super.dwrBaseSave(bean.getId());
	}
	
	public String xmlCreate() throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("login xmlCreate..........");
		}
		HttpServletResponse response = getResponse();
		HttpServletRequest request = getRequest();
		String WEBPATH = "http://" + request.getHeader("Host")
				+ request.getContextPath();
		dictCategoryValueBeanList = dictCategoryValueService.queryListById(getId());
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml  version=\"1.0\" encoding=\"UTF-8\"?><root>");
		for (DictCategoryValueBean model : dictCategoryValueBeanList) {
			sb.append("<tree text=\"");
			sb.append(htmEncode(model.getName()));
			sb.append("\" ");
			sb.append(" action=\"javascript:showCategoryValue('");
			sb.append(model.getId());
			sb.append("','");
			sb.append(getId());
			sb.append("');\"  icon=\"");
			sb.append(WEBPATH);
			sb.append("/images/xloadtree/page_white_gear.gif\"/>");
		}
		sb.append("</root>");
		response.setContentType("text/xml; charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		try {
			response.getWriter().write(sb.toString());
			response.getWriter().flush();
			response.getWriter().close();
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public void setDictCategoryValueService(
			DictCategoryValueService dictCategoryValueService) {
		this.dictCategoryValueService = dictCategoryValueService;
	}
}
